package org.jerm.Interface.JGraph_Diagrama;

import org.jgraph.graph.AttributeMap;
import org.jgraph.graph.CellHandle;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.GraphContext;
import org.jgraph.graph.VertexView;

/**
 * A default view factory for a JGraph. This simple factory associate a given
 * cell class to a cell view. This is a javabean, just parameter it correctly in
 * order it meets your requirements (else subclass it or subclass
 * DefaultCellViewFactory). You can also recover the gpConfiguration of that
 * javabean via an XML file via XMLEncoder/XMLDecoder.
 *
 * #author Tiago Falcao
 * 
 * @author rvalyi, license of this file: LGPL as stated by the Free Software
 *         Foundation
 */
public class JGraphCellViewFactory extends DefaultCellViewFactory {
	
	public static final String VIEW_CLASS_KEY = "viewClassKey";
	
	public static final void setViewClass(AttributeMap map, String viewClass) {
		map.put(VIEW_CLASS_KEY, viewClass);
	}

	protected VertexView createVertexView(Object v) {
		try {
			DefaultGraphCell cell = (DefaultGraphCell) v;
			String viewClass = (String) cell.getAttributes().get(VIEW_CLASS_KEY);

			VertexView view = (VertexView) Thread.currentThread()
					.getContextClassLoader().loadClass(viewClass).newInstance();
			view.setCell(v);
			return view;
		} catch (Exception ex) {
		}
		return super.createVertexView(v);
	}
        
        
        // Override Superclass Method to Return Custom EdgeView
        protected EdgeView createEdgeView(Object cell) {

                // Return Custom EdgeView
                return new EdgeView(cell) {

                        /**
                         * Returns a cell handle for the view.
                         */
                        public CellHandle getHandle(GraphContext context) {
                                return new MyEdgeHandle(this, context);
                        }

                };
        }
}
